﻿/*
* 作 者: 槐尧
* 公 司: 墨宇天城
* 创建时间: 2016/3/3 16:40:29
* 说明:  
*/

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class FruitButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public delegate void OnAddFruitDelegate(int index, int fruitId);
    public OnAddFruitDelegate OnAddFruit;

    public int index;
    public int fruitId;
    private Text textup;
    private Text textdown;

    void Awake()
    {
        textup = this.transform.Find("Textup").GetComponent<Text>();
        textdown = this.transform.Find("Textdown").GetComponent<Text>();
        Button btn = GetComponent<Button>();
        btn.onClick.AddListener(OnButtonClick);
    }

    public void OnButtonClick()
    {
        if (OnAddFruit != null)
        {
            OnAddFruit(index,fruitId);
        }
        SoundController.Instance.PlaySoundFromResource("Sound/s" + index);
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        if(textup!=null)
        {
            textup.gameObject.SetActive(false);
        }
        if (textdown != null)
        {
            textdown.gameObject.SetActive(true);
        }
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (textup != null)
        {
            textup.gameObject.SetActive(true);
        }
        if (textdown != null)
        {
            textdown.gameObject.SetActive(false);
        }
    }
    public void SetText()
    {
        RefreshProbabilityText(FruitDataManager.Intstance.Getprobability(fruitId));
    }
    private void RefreshProbabilityText(int probability)
    {
        textup.text = probability.ToString();
        textdown.text = probability.ToString();
    }
}